package com.shiwaixiangcun.monitor.utils;

import com.shiwaixiangcun.core.utils.MonkeyUtils;
import com.shiwaixiangcun.core.utils.SpringUtils;
import com.shiwaixiangcun.monitor.service.EarlyWarningService;
import com.shiwaixiangcun.monitor.service.WorkOrderService;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2017/6/16.
 */
public class NumberGenerator {
    private final Object lock = new Object();
    private static final String DEFAULT_NUMBER_FORMAT = "00000";
    private static final String DEFAULT_START_NUMBER = "00001";

    private CacheManager cacheManager;

    public NumberGenerator(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    //工单编号
    public String generateWorkOrderNumber() {
        Cache cache = cacheManager.getCache(MsConstants.SYSTEM_NUMBER_CACHE_NAME);
        synchronized (lock) {
            Cache.ValueWrapper valueWrapper = cache.get(MsConstants.WORK_ORDER_NUMBER_CACHE_KEY);
            if (valueWrapper == null) {
                valueWrapper = initialWorkOrderNumber(cache);
            }
            String newNumber = generateNewNumber(String.valueOf(valueWrapper.get()));
            cache.put(MsConstants.WORK_ORDER_NUMBER_CACHE_KEY, newNumber);
            return newNumber;
        }
    }

    //预警编号
    public String generateWaringNumber(Long orgId ) {
        Cache cache = cacheManager.getCache(MsConstants.SYSTEM_NUMBER_CACHE_NAME);
        synchronized (lock) {
            Cache.ValueWrapper valueWrapper = cache.get(MsConstants.WRING_NUMBER_CAHE_KEY);
            if (valueWrapper == null) {
                valueWrapper = initialeWaringNumber(cache,orgId);
            }
            String newNumber = generateNewNumber(String.valueOf(valueWrapper.get()));
            cache.put(MsConstants.WRING_NUMBER_CAHE_KEY, newNumber);
            return newNumber;
        }
    }

    private Cache.ValueWrapper initialeWaringNumber(Cache cache,Long orgId) {
        EarlyWarningService service = SpringUtils.getBean(EarlyWarningService.class);
        assert service != null;
        String maxNumber = service.findCurrentMaxNumber(orgId);
        if (MonkeyUtils.isEmpty(maxNumber)) {
            maxNumber = DEFAULT_NUMBER_FORMAT;
        }
        cache.put(MsConstants.WRING_NUMBER_CAHE_KEY, maxNumber);
        Cache.ValueWrapper valueWrapper = cache.get(MsConstants.WRING_NUMBER_CAHE_KEY);
        return valueWrapper;
    }


    private Cache.ValueWrapper initialWorkOrderNumber(Cache cache) {
        WorkOrderService service = SpringUtils.getBean(WorkOrderService.class);
        assert service != null;
        String maxNumber = service.findCurrentMaxNumber();
        if (MonkeyUtils.isEmpty(maxNumber)) {
            maxNumber = DEFAULT_NUMBER_FORMAT;
        }
        cache.put(MsConstants.WORK_ORDER_NUMBER_CACHE_KEY, maxNumber);
        return cache.get(MsConstants.WORK_ORDER_NUMBER_CACHE_KEY);
    }


    private String generateNewNumber(String maxNumber) {
        if (MonkeyUtils.isEmpty(maxNumber)) {
            maxNumber = DEFAULT_START_NUMBER;
        }
        Integer intHao = Integer.parseInt(maxNumber);
        intHao++;
        DecimalFormat df = new DecimalFormat(DEFAULT_NUMBER_FORMAT);
        return df.format(intHao);
    }

}
